package app.android.kdictionary.adapter;

import java.util.ArrayList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import app.android.kdictionary.R;
import app.android.kdictionary.R.id;
import app.android.kdictionary.aidl.WebViewContent;

// 메뉴 리스트 adapter
public class WebAdapter extends BaseAdapter {
	private ArrayList<WebViewContent> mData = new ArrayList<WebViewContent>();
	private LayoutInflater inflater;
	
	public WebAdapter(LayoutInflater inflater) {
		this.inflater = inflater;
	}
	
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}
	
	public void addItem(final WebViewContent item) {
        mData.add(item);
        notifyDataSetChanged();
    }
	
	public void addItems(ArrayList<WebViewContent> items) {
        mData.addAll(items);
//        notifyDataSetChanged();
    }
	
	public void clear() {
		mData.clear();
		notifyDataSetChanged();
	}
	
	public void removeLastItem() {
		mData.remove(getCount()-1);
		notifyDataSetChanged();
	}
	
    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public WebViewContent getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    	WebHolder holder = null;
	    if (convertView == null) {
	    	convertView = inflater.inflate(R.layout.web_item, null);	
	    	holder = new WebHolder();
	    	holder.webView = (WebView)convertView.findViewById(id.webview);
	        convertView.setTag(holder);	
	    } else {
	    	holder = (WebHolder)convertView.getTag();	    		
	    }

	    
	    switch(mData.get(position).getType()) {
	    	case ADD :
	    	case NOTFOUND :
	    	case RESULT :
	    	    holder.webView.getSettings().setJavaScriptEnabled(true);
	    	    holder.webView.setBackgroundColor(0);
	    	    holder.webView.setVerticalScrollBarEnabled(false);
	    	    holder.webView.setKeepScreenOn(true);
	    	    
	    	    //holder.webView.setDrawingCacheBackgroundColor(0);
	    	    //holder.webView.setDrawingCacheEnabled(true);
	    	    holder.webView.loadData(mData.get(position).getWebViewString(), "text/html", "utf-8");
	    	    
	    	    holder.webView.setFocusable(false);
	    	    holder.webView.setClickable(false);
	    	    break;
	    	    
	    	case PROGRESS :
	    		holder.webView.loadUrl("file:///android_asset/loader.htm");
	    		break;
	    }

	    return convertView;
    }
}

